# -*- encoding: ISO-8859-1 -*- 
import random

class Pion():
    def __init__(self,parent="",x=0,y=0):
        self.parent=parent
        self.x=x
        self.y=y
        self.pos=[x,y]
        
class Ferraille(Pion):
    def __init__(self,parent,x,y):
        Pion.__init__(self, parent,x, y)
        
class Dalek(Pion):
    def __init__(self,parent,x,y):
        Pion.__init__(self, parent,x, y)
        
    def deplace(self,doc):
        if doc.x<self.x:
            self.x=self.x-1
        elif doc.x>self.x:
            self.x=self.x+1  
        if doc.y<self.y:
            self.y=self.y-1
        elif doc.y>self.y:
            self.y=self.y+1  
        self.pos=[self.x,self.y]
            
class Docteur(Pion):
    def __init__(self,parent,x,y):
        Pion.__init__(self, parent,x, y)
        
    def deplace(self,deltaX,deltaY):
        x=self.x+deltaX
        y=self.y+deltaY
        if x>-1 and x<self.parent.largeur:
            self.x=x
        if y>-1 and y<self.parent.hauteur:
            self.y=y
        self.pos=[self.x,self.y]
    
    def teleporte(self):
        ok=1
        while ok:
            ok=0
            x=random.randrange(self.parent.largeur)
            y=random.randrange(self.parent.hauteur)
            for i in self.parent.ferrailles:
                if i.x==x and i.y==y:
                    ok=1
            for i in self.parent.daleks:
                if i.x==x and i.y==y:
                    ok=1
        self.x=x
        self.y=y
        self.pos=[x,y]
        
    def zappe(self):
        if self.parent.zappeur:
            pos=[]
            x=self.x
            y=self.y
            for i in range(3):
                pos.append([x+i-1,y-1])
            for i in range(3):
                pos.append([x+i-1,y])
            for i in range(3):
                pos.append([x+i-1,y+1])                
            mort=[]
            for i in self.parent.daleks:
                if i.pos in pos:
                    mort.append(i)
            for i in mort:
                self.parent.daleks.remove(i)
            self.parent.zappeur=self.parent.zappeur-1
    
            
class Partie():
    def __init__(self,parent="",largeur=16,hauteur=12):
        self.parent=parent
        self.largeur=largeur
        self.hauteur=hauteur
        self.nbDalekNivo=10
        self.etat=1
        self.nivo=0
        self.zappeur=0
        self.daleks=[]
        self.ferrailles=[]
        self.doc=""
        self.points=0
        self.initNivo()
        
    def initNivo(self):
        self.nivo=self.nivo+1
        self.zappeur=self.zappeur+1
        self.ferrailles=[]
        self.creePions()
        
    def creePions(self):
        pos=[]
        x=random.randrange(self.largeur)
        y=random.randrange(self.hauteur)
        pos.append([x,y])
        self.doc=Docteur(self,x,y)
        n=self.nivo*self.nbDalekNivo
        for i in range(n):
            x=random.randrange(self.largeur)
            y=random.randrange(self.hauteur)
            if [x,y] in pos:
                i=i-1
            else:
                self.daleks.append(Dalek(self,x,y))
                pos.append([x,y])
        self.daleks.append(Dalek(self,0,0))
                
    def teleporte(self):
        self.doc.teleporte()
    def zappe(self):
        self.doc.zappe()
        
    def joueTour(self,deltaX=0,deltaY=0):
        self.doc.deplace(deltaX,deltaY)
        for i in self.daleks:
            i.deplace(self.doc)
        n1=len(self.daleks)
        self.evalueJeu()
        if self.etat:
            n2=len(self.daleks)
            self.points=self.points+(n1-n2)
            if n2==0:
                self.initNivo()
            
        
    def evalueJeu(self):
        for i in self.daleks:
            if i.pos==self.doc.pos:
                self.etat=0
                break
        if self.etat:
            mort=[]
            for i in self.daleks:
                for j in self.ferrailles:
                    if i.pos==j.pos:
                        if i not in mort:
                            mort.append(i)
                            self.points=self.points+1
            for i in mort:
                self.daleks.remove(i)
            mort=[]
            
            for i in self.daleks:
                for j in self.daleks:
                    if i!=j:
                        if i.pos==j.pos:
                            if i not in mort:
                                mort.append(i)
                                self.points=self.points+1
            
            for i in mort:
                self.ferrailles.append(Ferraille(self,i.x,i.y))
                self.daleks.remove(i)
        

        
if __name__ == '__main__':
    c=Partie()